﻿using System.IO;
using System.Linq;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Tasks;

namespace deploymenttools
{
    [TaskName("deployOnSharedFolder")]
    public class DeployOnSharedFolder : CopyTask 
    {
        //[TaskAttribute("backups"), StringValidator(AllowEmpty = false)]
        //public int NumberOfBackups { get; set; }

        //[TaskAttribute("suffix"), StringValidator(AllowEmpty = false)]
        //public string Suffix { get; set; }

        [TaskAttribute("username"), StringValidator(AllowEmpty = false)]
        public string Username { get; set; }

        [TaskAttribute("domain"), StringValidator(AllowEmpty = false)]
        public string Domain { get; set; }

        [TaskAttribute("password"), StringValidator(AllowEmpty = false)]
        public string Password { get; set; }

        [TaskAttribute("todir")]
        public virtual string NewToDirectory { get; set; }

        [TaskAttribute("originaltodir")]
        public override DirectoryInfo ToDirectory
        {
            get
            {
                return base.ToDirectory;
            }
            set
            {
                base.ToDirectory = value;
            }
        }


        protected override void Initialize()
        {
            RemoteCredentials.DoAs(Username, Domain, Password, Predeploy);
        }

        protected override void ExecuteTask()
        {
            try
            {
                RemoteCredentials.DoAs(Username, Domain, Password, Deploy);
            }
            catch
            {
                if (FailOnError)
                    throw;
            }
        }

        private void Predeploy()
        {
            ToDirectory = new DirectoryInfo(NewToDirectory);
            base.Initialize();
        }

        private void Deploy()
        {
            Log(string.Format("Deploy from {0} to {1}", CopyFileSet.BaseDirectory, ToDirectory));
            base.ExecuteTask();
           // DeleteFolders(ToDirectory, Suffix, NumberOfBackups);
        }

        private void Log(string message)
        {
            Project.Log(Level.Info, message);
        }

        //private void DeleteFolders(DirectoryInfo dirinfo, string prefix, int numToSkip)
        //{
        //    if (numToSkip == 0) return;
        //    var possibleDirs = dirinfo.EnumerateDirectories(prefix + "*", SearchOption.TopDirectoryOnly);
        //    var orderedDirs = possibleDirs.OfType<DirectoryInfo>().OrderByDescending(dir => DirNameToNum(dir, prefix));
        //    foreach (var dir in orderedDirs.Skip(numToSkip))
        //    {
        //        Log("Deleting " + dir.FullName);
        //        dir.Delete(true);
        //    }
        //}

        //private int DirNameToNum(DirectoryInfo dir, string prefix)
        //{
        //    var fullName = Path.Combine(dir.Parent.FullName, prefix);
        //    var subName = dir.FullName.Substring(fullName.Length);
        //    int res = int.Parse(subName);
        //    return res;
        //}
    }
}